<template>
  <div class='container'>
    <el-card>
      <el-row>
        <el-col :span="22"><div class="grid-content bg-purple"><span class="size">说明: 目前支持学科和关键字条件筛选</span></div></el-col>
        <el-col :span="2">
          <el-button type="success" size="mini" @click="$router.push('/questions/new')" icon="el-icon-edit" >新增试题</el-button>
        </el-col>
      </el-row>

      <el-form ref="form" :model="formInline" label-width="80px">
        <!-- 第一排 -->
        <el-row>
          <el-col :span="6">
            <el-form-item label="科学">
              <el-select style="display:block" v-model="formInline.subjectID" @change="changetwotags(formInline.subjectID)" placeholder="请选择">
                <el-option v-for="item in form" :key="item.id" :label="item.subjectName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="二级目录">
              <el-select style="display:block" v-model="formInline.twoLeveIDirectory" placeholder="请选择">
                <el-option v-for="item in twolist" :key="item.id" :label="item.directoryName" :value="item.directoryName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标签">
              <el-select style="display:block" v-model="formInline.tags" placeholder="请选择">
                <el-option v-for="item in tagslist" :key="item.id" :label="item.tagName" :value="item.tagName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="关键字" >
              <el-input v-model="formInline.keyword" placeholder="根据题干搜索" />
            </el-form-item>
          </el-col>
        </el-row>

       <!-- 第二排 -->
       <el-row>
          <el-col :span="6">
            <el-form-item label="试题类型">
              <el-select style="display:block" v-model="formInline.questionTyoe" placeholder="请选择">
                <el-option v-for="item in questionType" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="难度">
              <el-select style="display:block" v-model="formInline.difficulty" placeholder="请选择">
                <el-option v-for="item in difficulty" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="方向">
              <el-select style="display:block" v-model="formInline.direction" placeholder="请选择">
                <el-option v-for="(item,index) in direction" :key="index" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="录入人">
              <el-select style="display:block" v-model="formInline.creatorID" placeholder="请选择">
                <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
       </el-row>

       <!-- 第三排 -->
       <el-row>
          <el-col :span="6">
            <el-form-item label="题目备注">
              <el-input v-model="formInline.remarks" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="企业简称">
              <el-input v-model="formInline.shortName" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="城市">
              <el-col :span="12" style="padding-right: 5px">
                <el-select  v-model="formInline.province" @change="choseProvince(formInline.province)"  placeholder="请选择">
                  <el-option v-for="(item,index) in provinces" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-col>
              <el-col :span="12">
                <el-select v-model="formInline.city" placeholder="请选择">
                  <el-option v-for="(item,index) in citylist" :key="index" :label="item" :value="item"></el-option>
                </el-select>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :span="6" class="btn">
            <el-row >
              <el-button  @click="removeform">清除</el-button>
              <el-button  type="primary">搜索</el-button>
            </el-row>
          </el-col>
       </el-row>
      </el-form>

      <!-- 分割 -->
      <el-alert class="alert" show-icon type="info" :title="`数据一共有：${counts}条`" :closable="false" />
      <!-- 列表数据 -->
      <el-table
        :header-cell-style="{background:'#fafafa','border-bottom':'2px solid #e8e8e8'}"
        :data="listss"
        style="width: 100%">
        <el-table-column prop="number" label="试题编号" width="250" />
        <el-table-column prop="subject" label="学科" width="120" />
        <el-table-column prop="catalog" label="目录" width="120" />
        <el-table-column prop="questionType" label="题型" :formatter="formatterquestionType" width="120" />
        <el-table-column label="题干" prop="question" width="350">
          <template #default={row}>
            <span v-html="row.question"></span>
          </template>
        </el-table-column>
        <el-table-column prop="addDate" label="录入时间" width="200">
          <template #default={row}>
            {{ formatDate(row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="difficulty" :formatter="formatterdifficulty" label="难度" width="120" />
        <el-table-column prop="creator" label="录入人" width="120" />
        <el-table-column label="操作" width="250">
          <template #default={row}>
            <el-button type="primary" plain  icon="el-icon-view" circle @click="clickdialog(row)"></el-button>
            <el-button type="success" plain  icon="el-icon-edit" circle @click="routerPush(row.id)"></el-button>
            <el-button type="danger" plain  icon="el-icon-delete" circle @click="clickdel(row)"></el-button>
            <el-button type="warning" plain  icon="el-icon-check" circle @click="clickchoice(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区 -->
      <div class="block">
        <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[2, 5, 10]"
        :page-size="data.pagesize"
        layout="prev, pager, next,sizes, jumper"
        :total="counts">
        </el-pagination>
      </div>
       <!-- 弹层 -->
      <el-dialog v-if="showDialog" title="题目预览" :visible.sync="showDialog" width="50%">
        <Topicpreview
          :timudata="timudata"
          @close="showDialog = false"
          :questionType="questionType"
          :difficulty="difficulty"
        />
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { subList } from '@/api/hmmm/subjects.js' // 获取学科列表
import { list as mulu } from '@/api/hmmm/directorys.js' // 目录
import { list as tags } from '@/api/hmmm/tags.js' // 标签列表
import { difficulty, questionType, direction } from '@/api/hmmm/constants.js' // difficulty题目难度 questionType 题型 direction方向
import { list as lists } from '@/api/base/users.js' // 录入人列表
import { provinces, citys } from '@/api/hmmm/citys.js' // 获取城市
import { list as listss, remove, choiceAdd } from '@/api/hmmm/questions.js' // 表单数据
import Topicpreview from '@/module-hmmm/components/questions-preview' // 导入弹层
export default {
  components: {
    Topicpreview
  },
  data () {
    return {
      formInline: { // 学科城市联动
        twoLeveIDirectory: '',
        tags: '',
        city: ''
      },
      form: [], // 学科列表
      twolist: [], // 二级目录
      tagslist: [], // 标签
      questionType: [], // 题型
      difficulty: [], // 题目难度
      direction: [], // 方向
      list: [], // 录入人
      provinces: [], // 所有城市
      citylist: [], // 区县
      listss: [], // 基础题库列表
      counts: null, // 总条数
      data: { // 全部基础题库列表数据
        page: 1, // 当前页码
        pagesize: 5 // 页显示数据的条数
      },
      currentPage: 1, // 当前页面
      showDialog: false, // 弹层开关
      timudata: {} // 题目预览数据
    }
  },
  created () {
    this.getquestions()
    this.Arraydirection()
    this.ArrayquestionType()
    this.Arraydifficulty()
    this.getlists()
    this.getprovinces()
    this.getlistss()
  },
  methods: {
    // 获取学科
    async getquestions () {
      const res = await subList()
      // console.log(res)
      this.form = res.data.items
    },
    // 点击学科获取二级目录 标签
    async changetwotags (e) {
      this.formInline.twoLeveIDirectory = ''
      this.formInline.tags = ''
      const { data: { items } } = await mulu({ subjectID: e })
      // console.log(items)
      this.twolist = items
      const { data } = await tags()
      // console.log(data.items)
      this.tagslist = data.items
    },
    // 试题
    ArrayquestionType () {
      this.questionType = questionType
      // console.log(this.questionType)
    },
    // 题型
    Arraydifficulty () {
      this.difficulty = difficulty
    },
    // 试题类型枚举
    formatterquestionType (row, column, cellValue, index) {
      const obj = questionType.find(item => item.value === +cellValue)
      return obj.label
    },
    // 难度的枚举
    formatterdifficulty (row, column, cellValue, index) {
      const obj = difficulty.find(item => item.value === +cellValue)
      return obj.label
    },
    // 方向
    Arraydirection () {
      this.direction = direction
      // console.log(this.direction)
    },
    // 录入人
    async getlists () {
      const { data: { list } } = await lists()
      this.list = list
    },
    // 获取城市
    async getprovinces () {
      const res = await provinces()
      this.provinces = res
      // console.log(this.provinces)
    },
    // 市区联动
    choseProvince (e) {
      this.formInline.city = ''
      this.citylist = citys(e)
    },
    // 清空表单
    removeform () {
      this.formInline = {}
    },
    // 获取基础题库列表
    async getlistss () {
      const { data } = await listss(this.data)
      // console.log(data)
      this.counts = data.counts // 总条数
      this.listss = data.items // 列表数据
    },
    // 时间转换
    formatDate (date) {
      var dayjs = require('dayjs')
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
    },
    handleSizeChange (val) { // 调整页面显示数据的条数
      // console.log(val)
      this.data.pagesize = val
      this.getlistss()
    },
    handleCurrentChange (val) { // 当前在第几页
      // console.log(val)
      this.data.page = val
      this.getlistss()
    },
    // 点击修改跳转试题录入
    routerPush (id) {
      this.$router.push({
        path: '/questions/new',
        query: { id }
      })
    },
    // 删除
    async clickdel (data) {
      console.log(data)
      const res = await this.$confirm('确定要删除吗?').catch(error => error)
      if (res !== 'confirm') return
      await remove(data)
      this.$message.success('删除成功')
      if (this.listss.length === 1 && this.data.page > 1) this.data.page--
      this.getlistss()
    },
    // 加入精选
    clickchoice (val) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$router.push('/questions/choice')
        await choiceAdd(val.id, val.choiceState = 1)
        this.$message({
          type: 'success',
          message: '加入精选题库成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 数据父传子
    async clickdialog (value) {
      this.timudata = value
      this.showDialog = true
    }
  }
}
</script>

<style scoped>
.container {
  padding: 15px;
}
.alert {
  margin: 10px  0;
}
.size {
  font-size: 12px;
  color: pink;
}
.block {
  display: flex;
  justify-content: end;
  margin-top: 15px;
}
.el-button--mini {
  margin-bottom: 10px;
}
.btn {
  display: flex;
    justify-content: end;
}

</style>
